// Squad Management
// Desc: init (functions & key EH)
//-----------------------------------------------------------------------------
#include "\ca\editor\Data\Scripts\dikCodes.h"
#include "keyHandlerMacros.sqh"
#include "common.sqh"
//-----------------------------------------------------------------------------
// determine whether this script was called from the addon or a mission. 
_initViaAddon = false;
if (!isNil "_this") then
{
	// An 'addon' parameter will exist for addon version. For missions, use nothing or use any other param.
	if (typeName _this == typeName "") then
	{
		_initViaAddon = (_this == 'addon');
	};
};
// any mission-defined version of this system takes precedence, so ignore this one.
if (_initViaAddon && isClass (missionConfigFile >> "ICE_SquadManagementDialog")) exitWith
{
	// prevent addon sqdMgt\init.sqf being executed, but allow mission sqdMgt\init.sqf to be executed.
	if (!isMultiplayer) then {diag_log text 'init addon: ignored: a duplicate ICE_squadManagement was already found in mission.'}; // SP debug
};

diag_log text 'init sys: ICE_squadManagement';
//-----------------------------------------------------------------------------
if (isServer) then
{
	// monitor and remove empty groups periodically
	execVM _c_basePath(groupGC.sqf); // garbage collector
};

call compile preprocessFileLineNumbers _c_basePath(initFunctions.sqf);
//-----------------------------------------------------------------------------
// Valid keys array. The keys variable can be an empty array. Syntax [[dikCode, [shift,ctrl,alt]], ...]
_valid = !isNil "ICE_sqdMgt_keys";
if (_valid) then
{
	// perform type validations. Array must be nested and complete.
	_valid = (typeName ICE_sqdMgt_keys == typeName []);
	if (_valid) then
	{
		{
			_valid = (typeName _x == typeName []);
			if (_valid) then
			{
				_valid = 
					(typeName (_x select 0) == typeName 5) &&
					(typeName (_x select 1) == typeName []);
			};
			if (!_valid) exitWith {};
		} forEach ICE_sqdMgt_keys;
	};
};

// if invalid array or nil key specified, then provide a default. Empty array is valid for 'no keys'.
if (!_valid) then
{
	if (!isNil "ICE_sqdMgt_keys") then
	{
		diag_log format ["SqdMgt: Error: ICE_sqdMgt_keys is invalid: %1", ICE_sqdMgt_keys];
	};

	if (count actionKeys "NetworkPlayers" > 0) then
	{
		ICE_sqdMgt_keys = [[(actionKeys "NetworkPlayers") select 0, [true, false, false]]]; // usually shift+P
	}
	else
	{
		ICE_sqdMgt_keys = [[DIK_P, [true, false, false]]]; // shift+P
	};
};

// If defined, set up key EH to activate dialog directly.
if (count ICE_sqdMgt_keys > 0) then
{
	ICE_c_addKeyDownEH(_null,"_this call ICE_sqdMgt_keyDown");
};
